package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    /**
     * 新增逻辑
     * @param appointment
     */
    @Override
    public void save(Appointment appointment) {

        // 预约中状态
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        // 创建时间
        appointment.setCreateTime(new Date());

        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    /**
     * 编辑逻辑
     * @param appointment
     */
    @Override
    public void update(Appointment appointment) {
        Appointment oldObj = this.get(appointment.getId());
        // 如果状态不是预约中,则不能进行编辑
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("该状态不为预约中,无法进行编辑");
        }

        // 处理特殊字段
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
                throw new BusinessException("该状态不为预约中,无法进行删除");
            }
            appointmentMapper.deleteByPrimaryKey(dictId);

            // 如果使用硬删除操作 要将后续结算单中预约单id改为null
            // 建议使用软删除
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override
    public void cancel(Long id) {
        // (合理性校验) 只有预约中的预约单才可以进行取消
        Appointment oldObj = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("该预约不在预约中,无法进行取消");
        }
        appointmentMapper.changStatus(id, Appointment.STATUS_CANCEL);
    }

    /**
     * 软删除操作
     * @param ids
     */
    @Override
    public void softRemove(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
                throw new BusinessException("该状态不为预约中,无法进行删除");
            }
            appointmentMapper.softRemoveById(dictId);
        }
    }


    /**
     * 到店操作
     * @param id
     */
    @Override
    public void arrival(Long id) {
        // (合理性校验)只有预约中的预约单才可以现在到店操作
        Appointment oldObj = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("该状态不为预约中,无法选择到店操作");
        }
        appointmentMapper.arrival(id, Appointment.STATUS_ARRIVAL, new Date());
    }


    /**
     * 修改状态
     * @param appointmentId
     * @param statusSettle
     */
    @Override
    public void changStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.changStatus(appointmentId,statusSettle);
    }


    /**
     * 服务结算单的删除按钮操作
     * @param appointmentId
     * @param statusCancel
     */
    @Override
    public void updateAppointmentStatus(Long appointmentId, Integer statusCancel) {
        appointmentMapper.updateAppointmentStatus(appointmentId, statusCancel);
    }
}
